package cn.youqu.happy.module.youqu.service.systemActivity;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.youqu.happy.module.youqu.controller.admin.systemActivity.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.systemActivity.SystemActivityDO;
import cn.youqu.happy.framework.common.pojo.PageResult;

import cn.youqu.happy.module.youqu.convert.systemActivity.SystemActivityConvert;
import cn.youqu.happy.module.youqu.dal.mysql.systemActivity.SystemActivityMapper;

import static cn.youqu.happy.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.youqu.happy.module.youqu.enums.ErrorCodeConstants.*;

/**
 * 系统活动模板 Service 实现类
 *
 * @author 王其岩  20112001045
 */
@Service
@Validated
public class SystemActivityServiceImpl implements SystemActivityService {

    @Resource
    private SystemActivityMapper systemActivityMapper;

    @Override
    public Integer createSystemActivity(SystemActivityCreateReqVO createReqVO) {
        // 插入
        SystemActivityDO systemActivity = SystemActivityConvert.INSTANCE.convert(createReqVO);
        systemActivityMapper.insert(systemActivity);
        // 返回
        return systemActivity.getId();
    }

    @Override
    public void updateSystemActivity(SystemActivityUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSystemActivityExists(updateReqVO.getId());
        // 更新
        SystemActivityDO updateObj = SystemActivityConvert.INSTANCE.convert(updateReqVO);
        systemActivityMapper.updateById(updateObj);
    }

    @Override
    public void deleteSystemActivity(Integer id) {
        // 校验存在
        this.validateSystemActivityExists(id);
        // 删除
        systemActivityMapper.deleteById(id);
    }

    public void validateSystemActivityExists(Integer id) {
        if (systemActivityMapper.selectById(id) == null) {
            throw exception(SYSTEM_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public SystemActivityDO getSystemActivity(Integer id) {
        return systemActivityMapper.selectById(id);
    }

    @Override
    public List<SystemActivityDO> getSystemActivityList(Collection<Integer> ids) {
        return systemActivityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SystemActivityDO> getSystemActivityPage(SystemActivityPageReqVO pageReqVO) {
        return systemActivityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SystemActivityDO> getSystemActivityList(SystemActivityExportReqVO exportReqVO) {
        return systemActivityMapper.selectList(exportReqVO);
    }

}
